import Vue from 'vue'
import Router from 'vue-router'
import Index from 'views/index.vue'
import Home from 'views/home/Home.vue'
import Choicespecial from 'views/home/choices/choice-special.vue'
import Choicesingle from 'views/home/choices/choice-single.vue'

import Classify from 'views/classify/Classify.vue'
import Productlist from 'views/productList/product-list.vue'
import ProductDetails from 'views/productDetails/product-details.vue'

import Login from 'views/login/login.vue'
import Register from 'views/register/register.vue'
import Shopcart from 'views/shopCart/Shopcart.vue'
import Mine from 'views/mine/Mine.vue'
import Error from 'views/Error.vue'


Vue.use(Router)

const router = new Router({
    mode: 'history',
    routes: [
        {
            path: '/',
            redirect: '/home/choicespecial',
            meta: {
                show: true               
            }
        },
        {
            path: '/home',
            component: Home,
            redirect: '/home/choicespecial',
            children:[
                {
                    path:'/home/choicespecial',
                    component: Choicespecial,
                    meta: {
                        show: true               
                    }
                },
                {
                    path:'/home/choicesingle',
                    component: Choicesingle,
                    meta: {
                        show: true               
                    }
                }
            ],
  
        },
        {
            path: '/classify/:id',
            component: Classify,           
            meta: {
                show: true               
            }
        },
        {
            path: '/shopCart',
            component: Shopcart,
            meta: {
                show: true ,
                auth: true                         
            }
        },
        {
            path: '/mine',
            component: Mine,
            meta: {
                show: true               
            }
        },
        {
            path: '/login',
            component: Login,
            meta: {
                show: false 
            }
        },
        {
            path: '/register',
            component: Register,
            meta: {
                show: false               
            }
        },
        {
            path:'/product/:id', //商品列表
            component:Productlist
        },
        {
            path:'/details',
            component:ProductDetails,
        },
        {
            path: '/error',
            component: Error,
            meta: {
                show: true               
            }
        },
        {
            path: '**',
            component: Error,
            meta: {
                show: true               
            }
        }
    ]
})

router.beforeEach((to,from,next)=>{
    //需求登录判断
    if(to.meta.auth){
        //未登录
        var isLogin = sessionStorage.getItem('userName');
        if(isLogin){
            next();//跳转到目标页面
        }else{
          next('/login');//跳转到登录页面
        }
    }else{
      next();
    }
})

export default router